package com.example.csupermaket.controller;


import com.example.csupermaket.pojo.dto.AddressAddNewDTO;
import com.example.csupermaket.pojo.dto.AddressUpdateDTO;
import com.example.csupermaket.pojo.dto.ProductUpdateDTO;
import com.example.csupermaket.pojo.vo.AddressListItemVO;
import com.example.csupermaket.pojo.vo.ProductListItemVO;
import com.example.csupermaket.pojo.vo.ProductStandardVO;
import com.example.csupermaket.service.IAddressService;
import com.example.csupermaket.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/address")
@Validated
@Api(tags = "05.地址管理模块")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @PostMapping("/add-new")
    @ApiOperation("01.添加地址")
    @ApiOperationSupport(order = 100)
    private JsonResult addNew(AddressAddNewDTO addressAddNewDTO){

        addressService.addNew(addressAddNewDTO);

        return JsonResult.ok();
    }

    @PostMapping("/{aid:[0-9]+}/delete")
    @ApiOperation("02.删除地址")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name="aid",value = "地址id",required = true, dataType ="int")
    private JsonResult delete(@PathVariable int aid){
        addressService.deleteByAid(aid);
        return JsonResult.ok();

    }

    @PostMapping("/{aid:[0-9]+}/update")
    @ApiOperation("03.修改地址")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="aid",value = "地址id",required = true, dataType ="int")
    public JsonResult update(@Range(min = 1, message = "删除地址失败，ID值无效！")
                             @PathVariable int aid, @Valid AddressUpdateDTO addressUpdateDTO){
        addressService.update(aid,addressUpdateDTO);
        return JsonResult.ok();

    }

    @GetMapping("/{uid:[0-9]+}")
    @ApiOperation("根据uid查询地址详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "uid", value = "商品id", required = true, dataType = "long")
    })
    public JsonResult selectByUid(@Range(min = 1, message = "获取地址详情失败，uid无效！")
                                      @PathVariable int uid) {
        List<AddressListItemVO> addressList = addressService.selectByUid(uid);
        return JsonResult.ok(addressList);
    }

}
